/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: DefaultGanttPaintManager.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.preference;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.foliage.rcp.ui.graphics.ColorCache;
import org.foliage.rcp.ui.widgets.GanttRange;
import org.foliage.rcp.ui.widgets.GanttTask;

public class DefaultGanttPaintManager implements IGanttPaintManager {

	@Override
	public void drawTask(GC gc, GanttTask task, IGanttSettings settings, IGanttColorManager colorManager, boolean isSelected) {
		Rectangle bounds = task.getBounds();
		Font oldFont = gc.getFont();
		Font newFont = null;
		{
			if (isSelected) {
				gc.setLineStyle(SWT.LINE_DOT);
			}
			gc.setForeground(ColorCache.getColor(160, 160, 160));
			gc.setBackground(ColorCache.getColor(240, 240, 240));
			gc.drawRectangle(bounds);
			gc.fillRectangle(bounds.x + 1, bounds.y + 1, bounds.width - 1, bounds.height - 1);
			gc.setLineStyle(SWT.LINE_SOLID);
		}
		if (task.getBounds().width > 50) {
			{
				FontData[] data = oldFont.getFontData();
				data[0].setStyle(SWT.BOLD);
				data[0].setHeight(8);
				newFont = new Font(Display.getDefault(), data);
				gc.setFont(newFont);
				gc.setForeground(ColorCache.getBlack());
			}
			{
				int taskWidth = bounds.width;
				Point point = gc.stringExtent(task.getText());
				int textWidth = point.x;
				if (task.getImage() != null && !task.getImage().isDisposed()) {
					ImageData data = task.getImage().getImageData();
					textWidth = textWidth + 2 + data.width;
					int textLowerX = bounds.x + (taskWidth - textWidth) / 2;
					gc.drawImage(task.getImage(), textLowerX, bounds.y + 4);
					gc.drawString(task.getText(), textLowerX + data.width + 2, bounds.y + 4);
				} else {
					int textLowerX = bounds.x + (taskWidth - textWidth) / 2;
					gc.drawString(task.getText(), textLowerX, bounds.y + 4);
				}
			}
			newFont.dispose();
		}
		if (task.getBounds().width > 120) {
			{
				FontData[] data = oldFont.getFontData();
				data[0].setStyle(SWT.NONE);
				data[0].setHeight(7);
				newFont = new Font(Display.getDefault(), data);
				gc.setFont(newFont);
				gc.setForeground(ColorCache.getBlack());
			}
			{
				int taskWidth = bounds.width;
				DateFormat format = new SimpleDateFormat("HHmm");
				format.setTimeZone(settings.getTimeZone());
				String lowerCalendarString = format.format(task.getLowerCalendar().getTime());
				String upperCalendarString = format.format(task.getUpperCalendar().getTime());
				Point point = gc.stringExtent(upperCalendarString);
				gc.drawString(lowerCalendarString, bounds.x + 2, bounds.y + 1);
				gc.drawString(upperCalendarString, bounds.x + taskWidth - point.x - 2, bounds.y + 1);
			}
			newFont.dispose();
		}
		gc.setFont(oldFont);
	}

	@Override
	public void drawRange(GC gc, GanttRange range, IGanttSettings settings, IGanttColorManager colorManager) {
		Rectangle bounds = range.getBounds();
		gc.setForeground(ColorCache.getColor(0, 238, 0));
		gc.setBackground(ColorCache.getColor(0, 238, 0));
//		gc.fillGradientRectangle(bounds.x, bounds.y, bounds.width, bounds.height, false);
		gc.fillRectangle(bounds);
	}

	@Override
	public void drawTooltip(GC gc, GanttTask task, IGanttSettings settings, IGanttColorManager colorManager, Shell shell) {
		int width = 300;
		int height = 120;
		Font oldFont = gc.getFont();
		Font newFont = null;
		shell.setSize(width, height);
		{
			gc.setForeground(ColorCache.getColor(160, 160, 160));
			gc.setBackground(ColorCache.getWhite());
			gc.fillRectangle(0, 0, width, height);
			gc.drawRectangle(0, 0, width - 1, height - 1);
		}
		{
			gc.setForeground(ColorCache.getBlack());
			{
				FontData[] data = oldFont.getFontData();
				data[0].setHeight(11);
				data[0].setStyle(SWT.BOLD);
				newFont = new Font(Display.getDefault(), data);
				gc.setFont(newFont);
			}
			gc.drawString("航班：" + task.getText(), 10, 10);
			newFont.dispose();
			{
				FontData[] data = oldFont.getFontData();
				data[0].setHeight(9);
				newFont = new Font(Display.getDefault(), data);
				gc.setFont(newFont);
			}
			gc.drawString("航线：XXX - XXX", 10, 40);
			DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			format.setTimeZone(settings.getTimeZone());
			gc.drawString("时间：" + format.format(task.getLowerCalendar().getTime()) + " - " + format.format(task.getUpperCalendar().getTime()), 10, 55);
			gc.drawString("机型：XXX", 10, 70);
			gc.drawString("备注：XXX", 10, 85);
			newFont.dispose();
		}
		gc.setFont(oldFont);
	}

}
